<@s.token name="${m}"/>
<div class="eui-panel eui-layout" data-options="fit:true">
	<div class="datagrid-toolbar" data-options="region:'north',height:'auto'" style="padding:8px">
		<a href="javascript:void(0)" class="eui-linkbutton" iconCls="icon-save" plain="true" id="savebtn${m}">保存</a>
		<a href="javascript:void(0)" class="eui-linkbutton" iconCls="icon-expand" plain="true" id="expandbtn${m}">展开</a>
		<a href="javascript:void(0)" class="eui-linkbutton" iconCls="icon-collapse" plain="true" id="collapsebtn${m}">折叠</a>
		<input class="eui-textbox" style="width:120px" name="criteria.expr['rolename_like']"><a href="javascript:void(0)" class="eui-linkbutton" iconCls="icon-search" plain="true" id="searchbtn">查询</a>
	</div>
	<div class="eui-panel" data-options="region:'center',border:false">
		<ul class="eui-tree" id="tree${m}"
			data-options="url:'${path}/eframe/rights/etbrole/role!getAllPosi.action?roleId=${roleId}',
			method:'post',
			animate:false,
			cascadeCheck:false,
			checkbox:true,
			lines:true,
			onContextMenu:addMenu,
			onBeforeSelect:processSelector,
			onBeforeCheck:posiFilter,
			onBeforeLoad:loadOneTime,
			onLoadSuccess:recordCheckedData">
		</ul>
		<div id="checkedMenu${m}" class="eui-menu" style="width:120px;">
			<div data-options="iconCls:'icon-ok'" id="checkNextLevel${m}">选中下一级</div>
			<div data-options="iconCls:'icon-remove'" id="uncheckNextLevel${m}">撤消下一级</div>
			<div class="menu-sep"></div>
			<div data-options="iconCls:'icon-ok'" id="checkChildren${m}">选中所有子集</div>
			<div data-options="iconCls:'icon-remove'" id="uncheckChildren${m}">撤消所有子集</div>
		</div>
	</div>
</div>
<script>
	//添加需要传递的参数
	function loadOneTime(node, param) {
		if(node) return false;
	}
	var originalIds = new List();
	function recordCheckedData() {
		new List($("#tree${m}").tree("getChecked")).each(function(data){
			originalIds.add(data.id);
		});
	}
	function processSelector(node) {
		if(node.checked) {
			$("#tree${m}").tree('uncheck',node.target);
		} else {
			$("#tree${m}").tree('check',node.target);
		}
		return true;
	}
	function posiFilter(node, checked) {
		if(node.attributes.nodeType == "position") {
			return true;
		} else {
			return false;
		}
	}
	$("#expandbtn${m}").click(function(){
		$("#tree${m}").tree('expandAll');
	});
	$("#collapsebtn${m}").click(function(){
		$("#tree${m}").tree('collapseAll');
	});
	$("#savebtn${m}").click(function(){
		var checkedIds = new List();
		new List($("#tree${m}").tree("getChecked")).each(function(data){
			checkedIds.add(data.id);
		});
		var delPosiIds = originalIds.getMyUniquePart(checkedIds);
		var addPosiIds = originalIds.getMyLackPart(checkedIds);
		var data = {roleId:"${roleId}", addPosiIds: addPosiIds.toString(), delPosiIds: delPosiIds.toString()};
		eui.ajax({
			url: "${path}/eframe/rights/etbrole/role!savePosi.action",
			token: '${m}',
			data: data,
			success:function(data){
				if(data == "token") {
					eui.msg("正在处理中...");
				} else {
					eui.alert("已保存",function(){
						$("#positab").panel("refresh");
					});
				}
			}
		});
	});
	
	//显示树菜单
	function addMenu(e,node) {
		if(node.checked) {
			$("#tree${m}").tree('uncheck',node.target);
		} else {
			$("#tree${m}").tree('check',node.target);
		}
		e.preventDefault();
		$('#tree${m}').tree('select',node.target);
		$('#checkedMenu${m}').menu('show',{
			left: e.pageX,
			top: e.pageY
		});
	}
	
	//选中下一级
	$("#checkNextLevel${m}").click(function(){
		var node = $("#tree${m}").tree('getSelected');
		var children = $("#tree${m}").tree('getNextLevelNodes', node.target);
		new List(children).each(function(e){
			$("#tree${m}").tree('check', e.target);
		});
	});
	//选中所有子集
	$("#checkChildren${m}").click(function(){
		var node = $("#tree${m}").tree('getSelected');
		var children = $("#tree${m}").tree('getChildren', node.target);
		new List(children).each(function(e){
			$("#tree${m}").tree('check', e.target);
		});
	});
	//撤消下一级
	$("#uncheckNextLevel${m}").click(function(){
		var node = $("#tree${m}").tree('getSelected');
		var children = $("#tree${m}").tree('getNextLevelNodes', node.target);
		new List(children).each(function(e){
			$("#tree${m}").tree('uncheck', e.target);
		});
	});
	//撤消所有子集
	$("#uncheckChildren${m}").click(function(){
		var node = $("#tree${m}").tree('getSelected');
		var children = $("#tree${m}").tree('getChildren', node.target);
		new List(children).each(function(e){
			$("#tree${m}").tree('uncheck', e.target);
		});
	});
</script>